﻿package com.game.data.vo
{
	import base.BaseHero;
	import base.BaseVO;
	
	import com.dusk.util.ArrayUtil;
	import com.dusk.util.MathUtil;
	import com.dusk.util.RandomUtil;
	import com.dusk.util.UtilBase;
	import com.game.data.constant.AttributeName;
	import com.hurlant.util.Base64;
	
	import config.Config;
	
	//noinspection ReservedWordAsName
	public class EquipmentVO extends BaseVO
	{
		
		public function EquipmentVO()
		{
			this.initEupData(this.type);
			this.initFiveElement();
		}
		
		/*-------------
		目前的装备属性：
		展示ID-showID
		
		血量-hp
		回血-hpHeal
		魔法-mp
		回魔-mpHeal
		攻击-atk
		防御-def
		暴击-crit
		韧性-toughness
		闪避-miss
		命中-hit
		嗜血-bloodThirst
		嗜魔-mpThirst
		爆伤-critDamage
		爆抗-deCritDamage
		-------------*/
		
		//装备展示id
		public var showId:uint = 1;
		//幻化后的展示id
		public var illusionId:uint = 1;
		//装备中文名称
		public var name:String = "";
		//装备类型（英文代号）
		public var type:String = "";
		//对应使用的角色名称，悟空唐僧八戒沙僧白龙
		public var user:String = "";
		//装备品质
		public var quality:String = "";
		//装备颜色
		public var color:uint = 0xFFFFFF;
		//装备类型（中文名称）
		public var type_cn:String = "";
		//装备介绍
		public var introduction:String = "";
		//强化数据
		public var strengthData:Object = {
			hp         : 0,
			hpHeal     : 0,
			mp         : 0,
			mpHeal     : 0,
			atk        : 0,
			def        : 0,
			crit       : 0,
			toughness  : 0,
			miss       : 0,
			hit        : 0,
			bloodThirst: 0,
			magicThirst: 0,
			magicDef   : 0,
			deMagicDef : 0,
			critDamage  : 0,
			deCritDamage: 0
		};
		//装备id,太极八卦就为tjbg
		public var equipId:String = "";
		//装备数量
		public var num:int = 1;
		/*-----------------常规装备属性，所有均为整数-------------------*/
		public var hp:int = 0;
		public var hpHeal:int = 0;
		public var mp:int = 0;
		public var mpHeal:int = 0;
		public var atk:int = 0;
		public var def:int = 0;
		public var crit:int = 0;
		public var toughness:int = 0;
		public var miss:int = 0;
		public var hit:int = 0;
		public var bloodThirst:int = 0;
		public var magicThirst:int = 0;
		public var magicDef:int = 0;
		public var deMagicDef:int = 0;
		public var critDamage:int = 0;
		public var deCritDamage:int = 0;
		/*-----------------常规装备属性，所有均为整数-------------------*/
		//强化等级
		public var strengthLevel:int = 0;
		//宝石槽
		public var gemSlot:Array = [];
		//等级，特指法宝等级
		public var level:int = 0;
		//成长率
		public var growthRate:Number = 0;
		//五行属性
		public var fiveElement:Array = [false, false, false, false, false];
		
		//是否可装备
		public var isCanEquip:Boolean = true;
		//是否可使用
		public var isCanUse:Boolean = false;
		//是否可以丢弃
		public var isCanDiscard:Boolean = false;
		//是否可以出售
		public var isCanSell:Boolean = true;
		
		private function get gc():Config
		{
			return Config.getIns();
		}
		
		/**
		 * 克隆装备
		 * @return 克隆后的装备
		 */
		public function clone():EquipmentVO
		{
			var vo:EquipmentVO = new EquipmentVO();
			var obj:Object = UtilBase.clone(cloneToObject());
			vo.updateFromObject(obj);
			return vo;
		}
		
		/**
		 * 设置五行
		 * @param arr 五行数组,[金,木,水,火,土]，默认值[]
		 */
		public function setFiveElement(arr:Array):void
		{
			if (!arr || arr.length != 5)
			{
				return;
			}
			fiveElement = arr;
		}
		
		/**
		 * 强化装备
		 * @param value 强化值
		 */
		public function upStrengthValue(value:int):void
		{
			strengthLevel = strengthLevel + value;
		}
		
		/**
		 * 判断是否有指定五行
		 * @param element 五行名称
		 * @return 是否有指定五行
		 */
		public function hasElement(element:String):Boolean
		{
			const map:Object = {
				"金": 0,
				"木": 1,
				"水": 2,
				"火": 3,
				"土": 4
			};
			if (map.hasOwnProperty(element))
			{
				return fiveElement[map[element]];
			}
			return false;
		}
		
		/**
		 * 获取五行字符串
		 * @return 五行字符串
		 */
		public function getFiveElementString():String
		{
			var elementString:String = "";
			if (fiveElement[0])
			{
				elementString += "金 ";
			}
			if (fiveElement[1])
			{
				elementString += "木 ";
			}
			if (fiveElement[2])
			{
				elementString += "水 ";
			}
			if (fiveElement[3])
			{
				elementString += "火 ";
			}
			if (fiveElement[4])
			{
				elementString += "土 ";
			}
			return elementString;
		}
		
		/**
		 * 获取升级数据
		 * @param sutraName 法宝名称
		 * @return 升级数据Object
		 */
		public function getGrowthDataByName(sutraName:String):Object
		{
			//对于新装备的拓展，只需要修改新的法宝名称和成长属性
			//属性数组格式为：[hp, mp, atk, def];
			var arr:Array;
			switch (sutraName)
			{
				case "kyl":
					arr = [20, 20, 4, 2];
					break;
				case "xhhl":
					arr = [30, 30, 5, 2];
					break;
				case "qyj":
					arr = [30, 50, 5, 2];
					break;
				case "hyzzs":
					arr = [50, 30, 5, 4];
					break;
				case "zjld":
					arr = [30, 30, 9, 2];
					break;
				case "syl":
					arr = [35, 35, 6, 3];
					break;
				case "lxj":
					arr = [40, 60, 7, 4];
					break;
				case "hywjs":
					arr = [65, 35, 8, 5];
					break;
				case "xhmt":
					arr = [60, 40, 15, 4];
					break;
				case "zsTimer":
					arr = [75, 45, 15, 4];
					break;
				case "mdhf":
					arr = [80, 50, 16, 5];
					break;
				case "jyhl":
					arr = [70, 39, 20, 4];
					break;
				case "qljfb":
					arr = [60, 60, 14, 5];
					break;
				case "yxfb":
					arr = [40, 40, 20, 0];
					break;
				case "sxfb":
					arr = [0, 0, 0, 30];
					break;
				case "tjbg":
					arr = [100, 80, 19, 10];
					break;
				case "lxfb":
					arr = [0, 0, 20, 0];
					break;
				case "stlp":
					arr = [75, 45, 18, 4];
					break;
				default:
					arr = [0, 0, 0, 0];
			}
			return {hp: arr[0], mp: arr[1], atk: arr[2], def: arr[3]};
		}
		
		/**
		 * 法宝升级
		 * @param growthData 升级数据
		 * @param isUpgrade 是否是升级，若为true则改变等级，否则不改变等级
		 */
		public function growth(growthData:Object, isUpgrade:Boolean = true):void
		{
			if (isUpgrade)
			{
				level++;
			}
			hp += growthData.hp * (growthRate + (hasElement("火") ? 1 : 0));
			mp += growthData.mp * (growthRate + (hasElement("水") ? 1 : 0));
			atk += growthData.atk * (growthRate + (hasElement("金") ? 1 : 0));
			def += growthData.def * (growthRate + (hasElement("土") ? 1 : 0));
			if (gc.isInBattle)
			{
				for each(var hero:BaseHero in this.gc.team.getHeroArray())
				{
					hero.updateMagicWeapon();
				}
			}
		}
		
		/**
		 * 导出存储数据
		 * @return 存储数据
		 */
		public function exportToSaveData():Object
		{
			var data:Object = {};
			CONFIG::DEBUG{
				data.name_cn = name;
			}
			data.illusionId = illusionId;
			data.equipId = equipId;
			data.num = num;
			data.strengthLevel = strengthLevel;
			data.gemSlot = gemSlot;
			data.level = level;
			data.growthRate = growthRate;
			data.fiveElement = fiveElement;
			
			
			data.hp = hp;
			data.hpHeal = hpHeal;
			data.mp = mp;
			data.mpHeal = mpHeal;
			data.atk = atk;
			data.def = def;
			data.crit = crit;
			data.toughness = toughness;
			data.miss = miss;
			data.hit = hit;
			data.bloodThirst = bloodThirst;
			data.magicThirst = magicThirst;
			data.magicDef = magicDef;
			data.deMagicDef = deMagicDef;
			data.critDamage = critDamage;
			data.deCritDamage = deCritDamage;
			return data;
		}
		
		/**
		 * 导入存储数据
		 * @param data 存储数据
		 */
		public function importFromSaveData(data:Object):void
		{
			illusionId = data.illusionId;
			equipId = data.equipId;
			num = data.num;
			strengthLevel = data.strengthLevel;
			gemSlot = data.gemSlot;
			level = data.level;
			growthRate = data.growthRate;
			fiveElement = data.fiveElement;
			
			hp = data.hp;
			hpHeal = data.hpHeal;
			mp = data.mp;
			mpHeal = data.mpHeal;
			atk = data.atk;
			def = data.def;
			crit = data.crit;
			toughness = data.toughness;
			miss = data.miss;
			hit = data.hit;
			bloodThirst = data.bloodThirst;
			magicThirst = data.magicThirst;
			magicDef = data.magicDef;
			deMagicDef = data.deMagicDef;
			critDamage = data.critDamage;
			deCritDamage = data.deCritDamage;
		}
		
		/**
		 * 设置成长率
		 * @param value
		 */
		public function setGrowthRate(value:Number):void
		{
			if (type != "zbfb")
			{
				return;
			}
			growthRate = MathUtil.decimal(value);
		}
		
		/**
		 * 获取成长率，若物品类型不是法宝则返回0
		 * @return
		 */
		public function getGrowthRate():Number
		{
			if (type != "zbfb")
			{
				return 0;
			}
			return MathUtil.decimal(growthRate, 2);
		}
		
		public function getLevel():int
		{
			return level;
		}
		
		/**
		 * 增加装备数量
		 * @param value 数量
		 */
		public function modifyNum(value:int):void
		{
			num += value;
		}
		
		public function getStrengthLevel():int
		{
			return strengthLevel;
		}
		
		public function getGemsAttribValue(attribName:String = ""):int
		{
			return 0;
		}
		
		public function getAttributeValue(attribName:String = "", all:Boolean = true, base:Boolean = false, strength:Boolean = false, gem:Boolean = false):int
		{
			if (!attribName)
			{
				return 0;
			}
			if (all)
			{
				base = strength = gem = true;
			}
			var value:int = 0;
			switch (attribName)
			{
				case AttributeName.HP:
					if (base)
					{
						value += hp;
					}
					break;
				case AttributeName.HP_HEAL:
					if (base)
					{
						value += hpHeal;
					}
					break;
				case AttributeName.MP:
					if (base)
					{
						value += mp;
					}
					break;
				case AttributeName.MP_HEAL:
					if (base)
					{
						value += mpHeal;
					}
					break;
				case AttributeName.ATK:
					if (base)
					{
						value += atk;
					}
					break;
				case AttributeName.DEF:
					if (base)
					{
						value += def;
					}
					break;
				case AttributeName.CRIT:
					if (base)
					{
						value += crit;
					}
					break;
				case AttributeName.TOUGHNESS:
					if (base)
					{
						value += toughness;
					}
					break;
				case AttributeName.HIT:
					if (base)
					{
						value += hit;
					}
					break;
				case AttributeName.MISS:
					if (base)
					{
						value += miss;
					}
					break;
				case AttributeName.BLOOD_THIRST:
					if (base)
					{
						value += bloodThirst;
					}
					break;
				case AttributeName.MAGIC_THIRST:
					if (base)
					{
						value += magicThirst;
					}
					break;
				case AttributeName.MAGIC_DEF:
					if (base)
					{
						value += magicDef;
					}
					break;
				case AttributeName.DE_MAGIC_DEF:
					if (base)
					{
						value += deMagicDef;
					}
					break;
				default:
			}
			if (strength)
			{
				value += getStrengthAttribValue(attribName);
			}
			if (gem)
			{
				value += getGemsAttribValue(attribName);
			}
			return value;
		}
		
		public function getStrengthAttribValue(attribName:String = ""):Number
		{
			switch (attribName)
			{
				case AttributeName.HP:
					return strengthData.hp * strengthLevel;
				case AttributeName.HP_HEAL:
					return strengthData.hpHeal * strengthLevel;
				case AttributeName.MP:
					return strengthData.mp * strengthLevel;
				case AttributeName.MP_HEAL:
					return strengthData.mpHeal * strengthLevel;
				case AttributeName.ATK:
					return strengthData.atk * strengthLevel;
				case AttributeName.DEF:
					return strengthData.def * strengthLevel;
				case AttributeName.CRIT:
					return strengthData.crit * strengthLevel;
				case AttributeName.TOUGHNESS:
					return strengthData.toughness * strengthLevel;
				case AttributeName.HIT:
					return strengthData.hit * strengthLevel;
				case AttributeName.MISS:
					return strengthData.miss * strengthLevel;
				case AttributeName.BLOOD_THIRST:
					return strengthData.bloodThirst * strengthLevel;
				case AttributeName.MAGIC_THIRST:
					return strengthData.mpThirst * strengthLevel;
				case AttributeName.MAGIC_DEF:
					return strengthData.magicDef * strengthLevel;
				case AttributeName.DE_MAGIC_DEF:
					return strengthData.deMagicDef * strengthLevel;
				default:
					return 0;
			}
		}
		
		public function findUsedByShowId():String
		{
			switch (this.showId)
			{
				case 100:
					return "zzs";
				case 101:
					return "bs";
				default:
					return "";
			}
		}
		
		public function isFashion():Boolean
		{
			return this.type == "zbsz";
		}
		
		public function getPreciousValue():int
		{
			switch (quality)
			{
				case "粗 糙":
					return 10;
				case "普 通":
					return 20;
				case "优 秀":
					return 40;
				case "精 良":
					return 80;
				case "史 诗":
					return 160;
				case "邪 灵":
					return 320;
				case "魂 器":
					return 640;
				case "传 说":
					return 1280;
				case "灵 器":
					return 2560;
				case "神 器":
					return 2560;
				default:
					return 0;
			}
		}
		
		public function missUpAttribute():void
		{
			const canMissUp:Array = ["zbfj", "zbwq", "zbsp", "zbfb"];
			if (!ArrayUtil.contains(canMissUp, type))
			{
				return;
			}
			var times:int;
			switch (quality)
			{
				case "粗 糙":
					times = 1;
					break;
				case "普 通":
					times = 2;
					break;
				case "优 秀":
					times = 5;
					break;
				case "精 良":
					times = 8;
					break;
				case "史 诗":
					times = 10;
					break;
				default:
					times = 0;
					break;
			}
			if (hp != 0)
			{
				hp += RandomUtil.randInt(0, 5 * times);
			}
			if (mp != 0)
			{
				mp += RandomUtil.randInt(0, 5 * times);
			}
			if (atk != 0)
			{
				atk += RandomUtil.randInt(0, 2 * times);
			}
			if (def != 0)
			{
				def += RandomUtil.randInt(0, 2 * times);
			}
		}
		
		private function initFiveElement():void
		{
			if (this.type != "zbfb")
			{
				fiveElement = [false, false, false, false, false];
				return;
			}
			fiveElement[RandomUtil.randInt(0, 4)] = true;//先随机获取一个属性
			for (var i:int = 0; i <= 4; i++)
			{
				if (RandomUtil.randBoolean(27))
				{
					fiveElement[i] = true;
				}
			}
		}
		
		private function initEupData(param1:String):void
		{
			if (param1 != "zbfb" || this.getGrowthRate() != 0)
			{
				return;
			}
			var range:Array;
			switch (equipId)
			{
				case "kyl":
					range = [0.7, 1];
					break;
				case "xhhl":
					range = [0.8, 1.1];
					break;
				case "qyj":
					range = [0.9, 1.4];
					break;
				case "hyzzs":
					range = [1.2, 1.6];
					break;
				case "zjld":
					range = [1.3, 1.8];
					break;
				case "syl":
					range = [0.9, 1.4];
					break;
				case "lxj":
					range = [1, 1.6];
					break;
				case "hywjs":
					range = [1.2, 1.8];
					break;
				case "zsTimer":
					range = [2.2, 2.5];
					break;
				case "jyhl":
					range = [2.5, 2.5];
					break;
				case "qljfb":
					range = [0.5, 2.1];
					break;
				case "mdhf":
					range = [2.7, 3];
					break;
				case "tjbg":
					range = [2.4, 3];
					break;
				case "xhmt":
					range = [1.8, 2];
					break;
				case "lxfb":
					range = [1.3, 1.4];
					break;
				case "sxfb":
					range = [1.9, 2];
					break;
				case "yxfb":
					range = [2.4, 2.5];
					break;
				case "stlp":
					range = [2.9, 3];
					break;
				case "hxyb":
					range = [0.9, 1.4];
					break;
				default:
					range = [0.1, 0.1];
			}
			this.setGrowthRate(RandomUtil.randNum(range[0], range[1], 1));
		}
	}
}
